package com.gin.kotlin.no_gay_alliance.activity

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.adapter.ReportHistoryAdapter
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingActivity
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.base.ViewUtils.Companion.visible
import com.gin.kotlin.no_gay_alliance.database.dao.ReportHistoryDao
import com.gin.kotlin.no_gay_alliance.databinding.ActivityReportHistoryBinding

class ReportHistoryActivity : AbstractBindingActivity<ActivityReportHistoryBinding>() {
    private lateinit var dao: ReportHistoryDao
    private lateinit var adapter: ReportHistoryAdapter

    override fun onCreate() {
        val b = this.b ?: return
        this.adapter = ReportHistoryAdapter(this,handler!!)
        b.recyclerView.adapter=adapter
        b.swipeRefreshLayout.setOnRefreshListener { query() }
        this.dao = App.INSTANCE.database.reportHistoryDao()
        query()
    }

    override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, menuInflater)
        menuInflater.inflate(R.menu.report_history,menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.mi_delete){
            //清空记录
            AlertDialogUtils.builder(this,"清空举报记录", listener = { _, which ->
                if (which==DialogInterface.BUTTON_POSITIVE) {
                    dao.clear()
                    adapter.clear()
                    b?.tvWarning?.visibility = View.VISIBLE
                }
            }).show()
        }

        return super.onOptionsItemSelected(item)
    }

    override fun onResume() {
        super.onResume()
        setToolbarTitle("举报记录")
    }
    private fun query(){
        val b = this.b ?: return
        b.swipeRefreshLayout.isRefreshing = true
        val data = dao.list()
        adapter.replaceData(data)
        b.swipeRefreshLayout.isRefreshing = false
        b.tvWarning.visibility = visible(data,true)
    }

    override fun handleIntent(intent: Intent) {
    }

    companion object{
        @JvmStatic
        fun start(context: Context) = context.startActivity(Intent(context, ReportHistoryActivity::class.java))
    }
}